package com.yelobblog.yelobblog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yelobblog.yelobblog.common.exception.ApiException;
import com.yelobblog.yelobblog.common.exception.ErrorEnum;
import com.yelobblog.yelobblog.common.untils.IPUtils;
import com.yelobblog.yelobblog.common.untils.Response;
import com.yelobblog.yelobblog.common.untils.ThreadLocalUtil;
import com.yelobblog.yelobblog.domain.Vo.articlesVo;
import com.yelobblog.yelobblog.domain.entity.articles;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import com.yelobblog.yelobblog.service.articlesService;
import com.yelobblog.yelobblog.common.untils.userStatusEnum;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/articles")
public class articlesController {
    @Resource
    private articlesService articlesService;

    @GetMapping("/get/{id}")
    public Response<articlesVo> selectById(@PathVariable Integer id, HttpServletRequest request) {
        String userIp = IPUtils.getClientIP(request);
        articlesService.recordViewCount(id, userIp);
        return Response.success(articlesService.selectById(id));
    }
    @GetMapping("/list")
    public Response<Page<articlesVo>> selectList(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        return Response.success(articlesService.selectList(pageNum, pageSize));
    }
    @GetMapping("/search")
    public Response<Page<articlesVo>> searchArticles(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String keyword) {
        return Response.success(articlesService.searchArticles(pageNum, pageSize, keyword));
    }
    @GetMapping("/listByCategoryId")
    public Response<Page<articlesVo>> selectListByCategoryId(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam Integer categoryId) {
        return Response.success(articlesService.selectListByCategoryId(pageNum, pageSize, categoryId));
    }
    @GetMapping("/listByUpdateTime")
    public Response<List<articlesVo>> selectListByUpdateTime() {
        return Response.success(articlesService.selectListByUpdateTime());
    }
    @GetMapping("/listByViewCount")
    public Response<List<articlesVo>> selectListByViewCount() {
        return Response.success(articlesService.selectListByViewCount());
    }
    @GetMapping("/userPublic")
    public Response<Page<articlesVo>> selectSelfPublic(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam Integer authorId) {
        return Response.success(articlesService.selectPublicByAuthorId(pageNum, pageSize,authorId));
    }
    @GetMapping("/selfPrivate")
    public Response<Page<articlesVo>> selectSelfPrivate(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        return Response.success(articlesService.selectPrivateByAuthorId(pageNum, pageSize));
    }
    @GetMapping("/selfDraft")
    public Response<Page<articlesVo>> selectSelfDraft(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        return Response.success(articlesService.selectDraftByAuthorId(pageNum, pageSize));
    }
    @GetMapping("/selfDeleted")
    public Response<Page<articlesVo>> selectSelfDeleted(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        return Response.success(articlesService.selectDeletedByAuthorId(pageNum, pageSize));
    }
    @PostMapping("/add")
    public Response<?> add(@RequestParam String title,@RequestParam String description,@RequestParam Integer categoryId) {
        articlesService.insert(title,description,categoryId);
        return Response.success();
    }
    @PutMapping("/update")
    public Response<?> update(@RequestBody articles articles) {
        articlesService.update(articles);
        return Response.success();
    }
    @DeleteMapping("/delete")
    public Response<?> delete(@RequestParam Integer id) {
        articlesService.deleteById(id);
        return Response.success();
    }
}
